package cz.schovjan.proj.controllers;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import cz.schovjan.proj.model.User;
import cz.schovjan.proj.model.UserLoginFormBean;
import cz.schovjan.proj.model.UserRegistrFormBean;
import cz.schovjan.proj.service.IUser;
import cz.schovjan.proj.util.StringUtil;

/**
 * Kontroler pro prihlasovani, odhlasovani a registraci.
 * @author schovjan
 * 
 */
@Controller
@RequestMapping("/logReg")
@SessionAttributes({"userLoginFormBean","userRegistrFormBean","user"}) //user se musi uvest taky, jinak se nepouzije
public class LogRegController extends Base {
	
	/** stranka s prihlasovacim/registracnim formularem */
	private static final String LOG_REG = "logReg";
	
	/** Service pro uzivatele.  */
	@Autowired
	private IUser users;

	@ModelAttribute("userLoginFormBean")
	public UserLoginFormBean createUserLoginFormBean() {

		return new UserLoginFormBean();
	}
	
	@ModelAttribute("userRegistrFormBean")
	public UserRegistrFormBean createUserRegistrFormBean() {

		return new UserRegistrFormBean();
	}
	
	
	/////////////////
	//OBSLUZNE FUKNCE ------------------------------------------------------------
	/////////////////
	
	
	/**
	 * Zobrazeni formulare pro prihlaseni.
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/login")
	public String login(ModelMap model) {
		model.addAttribute("login", true);

		return LOG_REG;
	}
	
	
	/**
	 * Ihned prihlasi uzivatele jako spravceho se vsemi jeho pravy.
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/loginNow")
	public String loginNow(ModelMap model) {
		User u = users.find("schovjan", "schovjan");
		
		if (u == null) {
			String message = MSG_LOGIN_FAILED;

			return "redirect:/error/"+message;
		}
		model.addAttribute("user", u);
		
		return "redirect:/band/summary";
	}
	
	
	/**
	 * Zobrazeni formulare pro registraci do systemu.
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/registr")
	public String registr(ModelMap model) {
		model.addAttribute("login", false);
		
		return LOG_REG;
	}
	
	
	/**
	 * Odhlasi uzivatele ze systemu.
	 * @param model
	 * @param s
	 * @return
	 */
	@RequestMapping(value="/logout")
	public String logout(ModelMap model, HttpSession s) {
		// mazu se session i z modelmap
		s.removeAttribute("user");
		model.remove("user");
		
		model.addAttribute("login", true);
		
		return LOG_REG;
	}
	
	
	/**
	 * Prihlaseni uzivatele.
	 * @param form
	 * @param model
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST, value="/loginSubmit")
	public String loginSubmit(@Valid UserLoginFormBean form, BindingResult result, ModelMap model) {
	
		if (result.hasErrors()) {
			model.addAttribute("login", true);
			return LOG_REG;
		}
		User u = users.find(form.getNick(), form.getPassword());
		
		if (u == null) {

			return "redirect:/error/"+StringUtil.toSecretString(MSG_LOGIN_FAILED);
		}
		model.addAttribute("user", u);
		
		return "redirect:/band/summary";
	}
	
	
	/**
	 * Registrace uzivatele
	 * @param form
	 * @param model
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST, value="/registrSubmit")
	public String regSubmit(@Valid UserRegistrFormBean form, BindingResult result, ModelMap model) {

		if (users.isExist(form.getNick())) {
			result.addError(new ObjectError("nickPouzit", MSG_NICK_USED));
		}
		
		if (!form.getPassword().equals(form.getPassword2())) {
			result.addError(new ObjectError("neshodaHesel", MSG_PASS_NOT_EQUAL));
		}
		
		if (result.hasErrors()) {
			model.addAttribute("login", false);
			return LOG_REG;
		}
		User u = users.add(
				form.getNick(),
				form.getPassword(),
				form.getName(),
				form.getSurname(),
				form.getSign(),
				form.getEmail()
			);
		model.addAttribute("user", u);
		sendEmailAboutRegister(u);
		
		return "redirect:/band/summary";
	}
	
	
	/**
	 * Fiktivni metoda.
	 * Na jeji volani je napojena advice pro posilani 
	 * emailu adminum o nove registraci uzivatele.
	 */
	private void sendEmailAboutRegister(User user) { } 
	
}
